package com.app.asktom.component;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.util.Log;

import com.app.asktom.impl.ImplHttp;

public class HttpHelper implements ImplHttp{
	ArrayList<String[]> get = new ArrayList<String[]>(); 
	ArrayList<NameValuePair> post = new ArrayList<NameValuePair>();
	protected boolean isPost;
	protected String url;
	protected String response;
	protected HttpPost httpPost;
	protected HttpGet httpGet;
	protected HttpResponse httpResponse;
	protected HttpClient httpClient = new DefaultHttpClient();
	
	@Override
	public void setPostParam(String key, String value) {
		Log.d("DEBUG", "POST: " + key + ":" + value);
		this.post.add(new BasicNameValuePair(key, value));
	}

	@Override
	public void setGetParam(String key, String value) {
		this.get.add(new String[]{key,value});
	}

	@Override
	public void setPost(boolean isPost) {
		this.isPost = isPost;
	}

	@Override
	public void setUrl(String url) {
		this.url = url;
	}

	@Override
	public void sendRequest() {
		StringBuilder sb = new StringBuilder();
		sb.append(this.url);
		if(this.get.size() > 0) {
			sb.append("?");
		}
		for(int i = 0; i < this.get.size(); i++) {
			sb.append(this.get.get(i)[0]);
			sb.append("=");
			sb.append(this.get.get(i)[1]);
			if(i < this.get.size() - 1) {
				sb.append("&");
			}
		}
		if(this.isPost) {
			httpPost = new HttpPost(sb.toString());
			try {
				this.httpPost.setEntity(new UrlEncodedFormEntity(this.post));
				 httpResponse = httpClient.execute(this.httpPost);
			} catch (UnsupportedEncodingException e) {
				this.response = "error: " + e.getMessage();
			} catch (ClientProtocolException e) {
				this.response = "error: " + e.getMessage();
			} catch (IOException e) {
				this.response = "error: " + e.getMessage();
			}
		} else {
			this.httpGet = new HttpGet(sb.toString());
			Log.d("DEBUG", sb.toString());
			try {
				httpResponse = httpClient.execute(this.httpGet);
			} catch (ClientProtocolException e) {
				this.response = "error:" + e.getMessage();
			} catch (IOException e) {
				this.response = "error:" + e.getMessage();
			}
		}
		StatusLine statusLine = httpResponse.getStatusLine(); 
		if(statusLine.getStatusCode() == HttpStatus.SC_OK) {
			ByteArrayOutputStream out = new ByteArrayOutputStream();		
			try {
				httpResponse.getEntity().writeTo(out);
				out.close();
				this.response = out.toString();
			} catch (IOException e) {
				this.response = "error:" + e.getMessage();
			}
		}
	}

	@Override
	public String getResponse() {
		return this.response;
	}

}
